
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <title>发布/订阅 · RabbitMQ in Chinese</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.0.0-pre.14">
        <meta name="author" content="Ping <yuanta11@gmil.com>">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="[4]Routing.html" />
    
    
    <link rel="prev" href="[2]Work_Queues.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://mr-ping.com" target="_blank" class="custom-link">瓶先生</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    RabbitMQ中文文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    RabbitMQ简介
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../description.html">
            
                <a href="../description.html">
            
                    
                    RabbitMQ能为你做些什么？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    安装
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../installation/Installing_on_Debian_Ubuntu.html">
            
                <a href="../installation/Installing_on_Debian_Ubuntu.html">
            
                    
                    在Debian及Ubuntu系统上进行安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../installation/Platforms_supported_by_RabbitMQ.html">
            
                <a href="../installation/Platforms_supported_by_RabbitMQ.html">
            
                    
                    RabbitMQ所支持的平台
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    AMQP协议
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../AMQP/AMQP_0-9-1_Model_Explained.html">
            
                <a href="../AMQP/AMQP_0-9-1_Model_Explained.html">
            
                    
                    AMQP 0.9.1 模型解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../AMQP/amqp-0-9-1-quickref.html">
            
                <a href="../AMQP/amqp-0-9-1-quickref.html">
            
                    
                    AMQP 0.9.1 快速参考指南
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    应用教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" >
            
                <span>
            
                    
                    Python版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="../tutorials_with_python/[1]Hello_World.html">
            
                <a href="../tutorials_with_python/[1]Hello_World.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="../tutorials_with_python/[2]Work_Queues.html">
            
                <a href="../tutorials_with_python/[2]Work_Queues.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.3" data-path="../tutorials_with_python/[3]Publish_Subscribe.html">
            
                <a href="../tutorials_with_python/[3]Publish_Subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.4" data-path="../tutorials_with_python/[4]Routing.html">
            
                <a href="../tutorials_with_python/[4]Routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.5" data-path="../tutorials_with_python/[5]Topics.html">
            
                <a href="../tutorials_with_python/[5]Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.6" data-path="../tutorials_with_python/[6]RPC.html">
            
                <a href="../tutorials_with_python/[6]RPC.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" >
            
                <span>
            
                    
                    C#版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="../tutorials_with_csharp/HelloWorld.html">
            
                <a href="../tutorials_with_csharp/HelloWorld.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.2" data-path="../tutorials_with_csharp/WorkQueue.html">
            
                <a href="../tutorials_with_csharp/WorkQueue.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.3" data-path="../tutorials_with_csharp/publish&subscribe.html">
            
                <a href="../tutorials_with_csharp/publish&subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.4" data-path="../tutorials_with_csharp/routing.html">
            
                <a href="../tutorials_with_csharp/routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.5" data-path="../tutorials_with_csharp/Topics.html">
            
                <a href="../tutorials_with_csharp/Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.6" data-path="../tutorials_with_csharp/rpc.html">
            
                <a href="../tutorials_with_csharp/rpc.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.3" >
            
                <span>
            
                    
                    golang版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.3.1" data-path="[1]Hello_World.html">
            
                <a href="[1]Hello_World.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.2" data-path="[2]Work_Queues.html">
            
                <a href="[2]Work_Queues.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.3.3" data-path="[3]Publish_Subscribe.html">
            
                <a href="[3]Publish_Subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.4" data-path="[4]Routing.html">
            
                <a href="[4]Routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.5" data-path="[5]Topics.html">
            
                <a href="[5]Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.6" data-path="[6]RPC.html">
            
                <a href="[6]RPC.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >发布/订阅</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <blockquote>
<p>&#x539F;&#x6587;&#xFF1A;<a href="https://www.rabbitmq.com/tutorials/tutorial-three-go.html" _target="blank">Publish/Subscribe</a><br>&#x72B6;&#x6001;&#xFF1A;&#x5F85;&#x6821;&#x5BF9;<br>&#x7FFB;&#x8BD1;&#xFF1A;<a href="https://bingjian-zhu.github.io/" _target="blank">Bingjian-Zhu</a><br>&#x6821;&#x5BF9;&#xFF1A;</p>
</blockquote>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/d/d0/CC-BY-SA_icon.svg" alt="CC-BY-SA"></p>
<h2 id="&#x53D1;&#x5E03;&#xFF0F;&#x8BA2;&#x9605;">&#x53D1;&#x5E03;&#xFF0F;&#x8BA2;&#x9605;</h2>
<p><strong>&#xFF08;&#x4F7F;&#x7528;Go&#x5BA2;&#x6237;&#x7AEF;&#xFF09;</strong></p>
<!--more-->
<p>&#x5728;<a href="https://bingjian-zhu.github.io/2019/09/01/RabbitMQ%E6%95%99%E7%A8%8B%EF%BC%88%E8%AF%91%EF%BC%89-Work-queues/" _target="blank">&#x4E0A;&#x7BC7;&#x6559;&#x7A0B;</a>&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x642D;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#xFF0C;&#x6BCF;&#x4E2A;&#x4EFB;&#x52A1;&#x53EA;&#x5206;&#x53D1;&#x7ED9;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;worker&#xFF09;&#x3002;&#x5728;&#x672C;&#x7BC7;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x505A;&#x7684;&#x8DDF;&#x4E4B;&#x524D;&#x5B8C;&#x5168;&#x4E0D;&#x4E00;&#x6837; &#x2014;&#x2014; &#x5206;&#x53D1;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x7ED9;&#x591A;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#x3002;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x88AB;&#x79F0;&#x4E3A;&#x201C;&#x53D1;&#x5E03;&#xFF0F;&#x8BA2;&#x9605;&#x201D;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x63CF;&#x8FF0;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x65E5;&#x5FD7;&#x7CFB;&#x7EDF;&#x3002;&#x5B83;&#x5305;&#x62EC;&#x4E24;&#x4E2A;&#x7A0B;&#x5E8F;&#x2014;&#x2014;&#x7B2C;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x8D1F;&#x8D23;&#x53D1;&#x9001;&#x65E5;&#x5FD7;&#x6D88;&#x606F;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x7A0B;&#x5E8F;&#x8D1F;&#x8D23;&#x83B7;&#x53D6;&#x6D88;&#x606F;&#x5E76;&#x8F93;&#x51FA;&#x5185;&#x5BB9;&#x3002;</p>
<p>&#x5728;&#x6211;&#x4EEC;&#x7684;&#x8FD9;&#x4E2A;&#x65E5;&#x5FD7;&#x7CFB;&#x7EDF;&#x4E2D;&#xFF0C;&#x6240;&#x6709;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x7684;&#x63A5;&#x6536;&#x65B9;&#x7A0B;&#x5E8F;&#x90FD;&#x4F1A;&#x63A5;&#x53D7;&#x6D88;&#x606F;&#x3002;&#x6211;&#x4EEC;&#x7528;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x63A5;&#x6536;&#x8005;&#xFF08;receiver&#xFF09;&#x628A;&#x65E5;&#x5FD7;&#x5199;&#x5165;&#x786C;&#x76D8;&#x4E2D;&#xFF0C;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x63A5;&#x53D7;&#x8005;&#xFF08;receiver&#xFF09;&#x628A;&#x65E5;&#x5FD7;&#x8F93;&#x51FA;&#x5230;&#x5C4F;&#x5E55;&#x4E0A;&#x3002;</p>
<p>&#x6700;&#x7EC8;&#xFF0C;&#x65E5;&#x5FD7;&#x6D88;&#x606F;&#x88AB;&#x5E7F;&#x64AD;&#x7ED9;&#x6240;&#x6709;&#x7684;&#x63A5;&#x53D7;&#x8005;&#xFF08;receivers&#xFF09;&#x3002;</p>
<h2 id="&#x4EA4;&#x6362;&#x673A;&#xFF08;exchanges&#xFF09;">&#x4EA4;&#x6362;&#x673A;&#xFF08;Exchanges&#xFF09;</h2>
<p>&#x524D;&#x9762;&#x7684;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x5230;&#x961F;&#x5217;&#x5E76;&#x4ECE;&#x4E2D;&#x53D6;&#x51FA;&#x6D88;&#x606F;&#x3002;&#x73B0;&#x5728;&#x662F;&#x65F6;&#x5019;&#x4ECB;&#x7ECD;RabbitMQ&#x4E2D;&#x5B8C;&#x6574;&#x7684;&#x6D88;&#x606F;&#x6A21;&#x578B;&#x4E86;&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x7B80;&#x5355;&#x7684;&#x6982;&#x62EC;&#x4E00;&#x4E0B;&#x4E4B;&#x524D;&#x7684;&#x6559;&#x7A0B;&#xFF1A;</p>
<ul>
<li>&#x53D1;&#x5E03;&#x8005;&#xFF08;producer&#xFF09;&#x662F;&#x53D1;&#x5E03;&#x6D88;&#x606F;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x3002;</li>
<li>&#x961F;&#x5217;&#xFF08;queue&#xFF09;&#x7528;&#x4E8E;&#x6D88;&#x606F;&#x5B58;&#x50A8;&#x7684;&#x7F13;&#x51B2;&#x3002;</li>
<li>&#x6D88;&#x8D39;&#x8005;&#xFF08;consumer&#xFF09;&#x662F;&#x63A5;&#x6536;&#x6D88;&#x606F;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x3002;</li>
</ul>
<p>RabbitMQ&#x6D88;&#x606F;&#x6A21;&#x578B;&#x7684;&#x6838;&#x5FC3;&#x7406;&#x5FF5;&#x662F;&#xFF1A;&#x53D1;&#x5E03;&#x8005;&#xFF08;producer&#xFF09;&#x4E0D;&#x4F1A;&#x76F4;&#x63A5;&#x53D1;&#x9001;&#x4EFB;&#x4F55;&#x6D88;&#x606F;&#x7ED9;&#x961F;&#x5217;&#x3002;&#x4E8B;&#x5B9E;&#x4E0A;&#xFF0C;&#x53D1;&#x5E03;&#x8005;&#xFF08;producer&#xFF09;&#x751A;&#x81F3;&#x4E0D;&#x77E5;&#x9053;&#x6D88;&#x606F;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x88AB;&#x6295;&#x9012;&#x5230;&#x961F;&#x5217;&#x3002;</p>
<p>&#x53D1;&#x5E03;&#x8005;&#xFF08;producer&#xFF09;&#x53EA;&#x9700;&#x8981;&#x628A;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x7ED9;&#x4E00;&#x4E2A;&#x4EA4;&#x6362;&#x673A;&#xFF08;exchange&#xFF09;&#x3002;&#x4EA4;&#x6362;&#x673A;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x5B83;&#x4E00;&#x8FB9;&#x4ECE;&#x53D1;&#x5E03;&#x8005;&#x65B9;&#x63A5;&#x6536;&#x6D88;&#x606F;&#xFF0C;&#x4E00;&#x8FB9;&#x628A;&#x6D88;&#x606F;&#x63A8;&#x9001;&#x5230;&#x961F;&#x5217;&#x3002;&#x4EA4;&#x6362;&#x673A;&#x5FC5;&#x987B;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x5904;&#x7406;&#x5B83;&#x63A5;&#x6536;&#x5230;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x662F;&#x5E94;&#x8BE5;&#x63A8;&#x9001;&#x5230;&#x6307;&#x5B9A;&#x7684;&#x961F;&#x5217;&#x8FD8;&#x662F;&#x662F;&#x591A;&#x4E2A;&#x961F;&#x5217;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x76F4;&#x63A5;&#x5FFD;&#x7565;&#x6D88;&#x606F;&#x3002;&#x8FD9;&#x4E9B;&#x89C4;&#x5219;&#x662F;&#x901A;&#x8FC7;&#x4EA4;&#x6362;&#x673A;&#x7C7B;&#x578B;&#xFF08;exchange type&#xFF09;&#x6765;&#x5B9A;&#x4E49;&#x7684;&#x3002;</p>
<p><img src="http://www.rabbitmq.com/img/tutorials/exchanges.png" alt=""></p>
<p>&#x6709;&#x51E0;&#x4E2A;&#x53EF;&#x4F9B;&#x9009;&#x62E9;&#x7684;&#x4EA4;&#x6362;&#x673A;&#x7C7B;&#x578B;&#xFF1A;<code>direct</code>, <code>topic</code>, <code>headers</code>&#x548C;<code>fanout</code>&#x3002;&#x6211;&#x4EEC;&#x5728;&#x8FD9;&#x91CC;&#x4E3B;&#x8981;&#x8BF4;&#x660E;&#x6700;&#x540E;&#x4E00;&#x4E2A; &#x2014;&#x2014; <code>fanout</code>&#x3002;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;<code>fanout</code>&#x7C7B;&#x578B;&#x7684;&#x4EA4;&#x6362;&#x673A;&#xFF0C;&#x547D;&#x540D;&#x4E3A;logs&#xFF1A;</p>
<pre><code class="lang-go">err = ch.ExchangeDeclare(
  <span class="hljs-string">&quot;logs&quot;</span>,   <span class="hljs-comment">// name</span>
  <span class="hljs-string">&quot;fanout&quot;</span>, <span class="hljs-comment">// type</span>
  <span class="hljs-literal">true</span>,     <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>,    <span class="hljs-comment">// auto-deleted</span>
  <span class="hljs-literal">false</span>,    <span class="hljs-comment">// internal</span>
  <span class="hljs-literal">false</span>,    <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,      <span class="hljs-comment">// arguments</span>
)
</code></pre>
<p><code>fanout</code>&#x4EA4;&#x6362;&#x673A;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4ECE;&#x540D;&#x5B57;&#x4E0A;&#x5C31;&#x80FD;&#x731C;&#x6D4B;&#x51FA;&#x6765;&#xFF0C;&#x5B83;&#x628A;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x7ED9;&#x5B83;&#x6240;&#x77E5;&#x9053;&#x7684;&#x6240;&#x6709;&#x961F;&#x5217;&#x3002;&#x8FD9;&#x6B63;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x65E5;&#x5FD7;&#x7CFB;&#x7EDF;&#x6240;&#x9700;&#x8981;&#x7684;&#x3002;</p>
<blockquote>
<h4 id="&#x4EA4;&#x6362;&#x5668;&#x5217;&#x8868;">&#x4EA4;&#x6362;&#x5668;&#x5217;&#x8868;</h4>
<p><code>rabbitmqctl</code>&#x80FD;&#x591F;&#x5217;&#x51FA;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x6240;&#x6709;&#x7684;&#x4EA4;&#x6362;&#x5668;&#xFF1A;<code>sudo rabbitmqctl list_exchanges</code>
&#x8FD9;&#x4E2A;&#x5217;&#x8868;&#x4E2D;&#x6709;&#x4E00;&#x4E9B;&#x53EB;&#x505A;<code>amq.*</code>&#x7684;&#x533F;&#x540D;&#x4EA4;&#x6362;&#x5668;&#x3002;&#x8FD9;&#x4E9B;&#x90FD;&#x662F;&#x9ED8;&#x8BA4;&#x521B;&#x5EFA;&#x7684;&#xFF0C;&#x4E0D;&#x8FC7;&#x8FD9;&#x65F6;&#x5019;&#x4F60;&#x8FD8;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4ED6;&#x4EEC;&#x3002;</p>
<h4 id="&#x533F;&#x540D;&#x7684;&#x4EA4;&#x6362;&#x5668;">&#x533F;&#x540D;&#x7684;&#x4EA4;&#x6362;&#x5668;</h4>
<p>&#x524D;&#x9762;&#x7684;&#x6559;&#x7A0B;&#x4E2D;&#x6211;&#x4EEC;&#x5BF9;&#x4EA4;&#x6362;&#x673A;&#x4E00;&#x65E0;&#x6240;&#x77E5;&#xFF0C;&#x4F46;&#x4ECD;&#x7136;&#x80FD;&#x591F;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x5230;&#x961F;&#x5217;&#x4E2D;&#x3002;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86;&#x547D;&#x540D;&#x4E3A;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;(&quot;&quot;)&#x7684;&#x533F;&#x540D;&#x4EA4;&#x6362;&#x673A;&#x3002;
&#x56DE;&#x60F3;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x662F;&#x5982;&#x4F55;&#x53D1;&#x5E03;&#x4E00;&#x5219;&#x6D88;&#x606F;&#xFF1A;</p>
<pre><code> err = ch.Publish(
  &quot;&quot;,     // exchange
  q.Name, // routing key
  false,  // mandatory
  false,  // immediate
  amqp.Publishing{
    ContentType: &quot;text/plain&quot;,
    Body:        []byte(body),
})
</code></pre><p>exchange&#x53C2;&#x6570;&#x5C31;&#x662F;&#x4EA4;&#x6362;&#x673A;&#x7684;&#x540D;&#x79F0;&#x3002;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#x4EE3;&#x8868;&#x9ED8;&#x8BA4;&#x6216;&#x8005;&#x533F;&#x540D;&#x4EA4;&#x6362;&#x673A;&#xFF0C;&#x6D88;&#x606F;&#x5C06;&#x4F1A;&#x6839;&#x636E;&#x6307;&#x5B9A;&#x7684;<code>routing_key</code>&#x5206;&#x53D1;&#x5230;&#x6307;&#x5B9A;&#x7684;&#x961F;&#x5217;&#x3002;</p>
</blockquote>
<p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x5230;&#x4E00;&#x4E2A;&#x5177;&#x540D;&#x4EA4;&#x6362;&#x673A;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-go">err = ch.ExchangeDeclare(
  <span class="hljs-string">&quot;logs&quot;</span>,   <span class="hljs-comment">// name</span>
  <span class="hljs-string">&quot;fanout&quot;</span>, <span class="hljs-comment">// type</span>
  <span class="hljs-literal">true</span>,     <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>,    <span class="hljs-comment">// auto-deleted</span>
  <span class="hljs-literal">false</span>,    <span class="hljs-comment">// internal</span>
  <span class="hljs-literal">false</span>,    <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,      <span class="hljs-comment">// arguments</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to declare an exchange&quot;</span>)

body := bodyFrom(os.Args)
err = ch.Publish(
  <span class="hljs-string">&quot;logs&quot;</span>, <span class="hljs-comment">// exchange</span>
  <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// routing key</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// mandatory</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// immediate</span>
  amqp.Publishing{
          ContentType: <span class="hljs-string">&quot;text/plain&quot;</span>,
          Body:        []<span class="hljs-keyword">byte</span>(body),
  })
</code></pre>
<h2 id="&#x4E34;&#x65F6;&#x961F;&#x5217;">&#x4E34;&#x65F6;&#x961F;&#x5217;</h2>
<p>&#x4F60;&#x8FD8;&#x8BB0;&#x5F97;&#x4E4B;&#x524D;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684;&#x961F;&#x5217;&#x540D;&#x5417;&#xFF08; hello&#x548C;task_queue&#xFF09;&#xFF1F;&#x7ED9;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#x547D;&#x540D;&#x662F;&#x5F88;&#x91CD;&#x8981;&#x7684;&#x2014;&#x2014;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x628A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#x6307;&#x5411;&#x6B63;&#x786E;&#x7684;&#x961F;&#x5217;&#x3002;&#x5982;&#x679C;&#x4F60;&#x6253;&#x7B97;&#x5728;&#x53D1;&#x5E03;&#x8005;&#xFF08;producers&#xFF09;&#x548C;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#x4E4B;&#x95F4;&#x5171;&#x4EAB;&#x540C;&#x961F;&#x5217;&#x7684;&#x8BDD;&#xFF0C;&#x7ED9;&#x961F;&#x5217;&#x547D;&#x540D;&#x662F;&#x5341;&#x5206;&#x91CD;&#x8981;&#x7684;&#x3002;</p>
<p>&#x4F46;&#x662F;&#x8FD9;&#x5E76;&#x4E0D;&#x9002;&#x7528;&#x4E8E;&#x6211;&#x4EEC;&#x7684;&#x65E5;&#x5FD7;&#x7CFB;&#x7EDF;&#x3002;&#x6211;&#x4EEC;&#x6253;&#x7B97;&#x63A5;&#x6536;&#x6240;&#x6709;&#x7684;&#x65E5;&#x5FD7;&#x6D88;&#x606F;&#xFF0C;&#x800C;&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x4E00;&#x5C0F;&#x90E8;&#x5206;&#x3002;&#x6211;&#x4EEC;&#x5173;&#x5FC3;&#x7684;&#x662F;&#x6700;&#x65B0;&#x7684;&#x6D88;&#x606F;&#x800C;&#x4E0D;&#x662F;&#x65E7;&#x7684;&#x3002;&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x505A;&#x4E24;&#x4EF6;&#x4E8B;&#x60C5;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x8FDE;&#x63A5;&#x4E0A;RabbitMQ&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x5168;&#x65B0;&#x7684;&#x3001;&#x7A7A;&#x7684;&#x961F;&#x5217;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x7684;&#x961F;&#x5217;&#x540D;&#xFF0C;&#x6216;&#x8005;&#x8BA9;&#x670D;&#x52A1;&#x5668;&#x4E3A;&#x6211;&#x4EEC;&#x9009;&#x62E9;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x7684;&#x961F;&#x5217;&#x540D;&#xFF08;&#x63A8;&#x8350;&#xFF09;&#x3002;</p>
<p>&#x5176;&#x6B21;&#xFF0C;&#x5F53;&#x4E0E;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumer&#xFF09;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#x5E94;&#x5F53;&#x88AB;&#x7ACB;&#x5373;&#x5220;&#x9664;&#x3002;
&#x5728;<code>amqp</code>&#x5BA2;&#x6237;&#x7AEF;&#x4E2D;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x5C06;&#x961F;&#x5217;&#x540D;&#x79F0;&#x4F5C;&#x4E3A;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#x63D0;&#x4F9B;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5177;&#x6709;&#x751F;&#x6210;&#x540D;&#x79F0;&#x7684;&#x975E;&#x6301;&#x4E45;&#x961F;&#x5217;&#xFF1A;</p>
<pre><code class="lang-go">q, err := ch.QueueDeclare(
  <span class="hljs-string">&quot;&quot;</span>,    <span class="hljs-comment">// name</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// delete when usused</span>
  <span class="hljs-literal">true</span>,  <span class="hljs-comment">// exclusive</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,   <span class="hljs-comment">// arguments</span>
)
</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x65F6;&#xFF0C;&#x961F;&#x5217;&#x5B9E;&#x4F8B;&#x5305;&#x542B;RabbitMQ&#x751F;&#x6210;&#x7684;&#x968F;&#x673A;&#x961F;&#x5217;&#x540D;&#x79F0;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5B83;&#x53EF;&#x80FD;&#x770B;&#x8D77;&#x6765;&#x50CF;<code>amq.gen-JzTY20BRgKO-HjmUJj0wLg</code>&#x3002;
&#x5F53;&#x58F0;&#x660E;&#x5B83;&#x7684;&#x8FDE;&#x63A5;&#x5173;&#x95ED;&#x65F6;&#xFF0C;&#x961F;&#x5217;&#x5C06;&#x88AB;&#x5220;&#x9664;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x88AB;&#x58F0;&#x660E;&#x4E3A;<code>exclusive</code>&#x3002;
&#x60A8;&#x53EF;&#x4EE5;&#x5728;<a href="https://www.rabbitmq.com/queues.html" _target="blank">guide on queues</a>&#x4E86;&#x89E3;&#x6709;&#x5173;<code>exclusive</code>&#x548C;&#x5176;&#x4ED6;&#x961F;&#x5217;&#x5C5E;&#x6027;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;</p>
<h2 id="&#x7ED1;&#x5B9A;&#xFF08;bindings&#xFF09;">&#x7ED1;&#x5B9A;&#xFF08;Bindings&#xFF09;</h2>
<p><img src="http://www.rabbitmq.com/img/tutorials/bindings.png" alt=""></p>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;<code>fanout</code>&#x4EA4;&#x6362;&#x673A;&#x548C;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#x3002;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x544A;&#x8BC9;&#x4EA4;&#x6362;&#x673A;&#x5982;&#x4F55;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x961F;&#x5217;&#x3002;&#x4EA4;&#x6362;&#x5668;&#x548C;&#x961F;&#x5217;&#x4E4B;&#x95F4;&#x7684;&#x8054;&#x7CFB;&#x6211;&#x4EEC;&#x79F0;&#x4E4B;&#x4E3A;&#x7ED1;&#x5B9A;&#xFF08;binding&#xFF09;&#x3002;</p>
<pre><code class="lang-go">err = ch.QueueBind(
  q.Name, <span class="hljs-comment">// queue name</span>
  <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// routing key</span>
  <span class="hljs-string">&quot;logs&quot;</span>, <span class="hljs-comment">// exchange</span>
  <span class="hljs-literal">false</span>,
  <span class="hljs-literal">nil</span>,
)
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;logs&#x4EA4;&#x6362;&#x673A;&#x5C06;&#x4F1A;&#x628A;&#x6D88;&#x606F;&#x6DFB;&#x52A0;&#x5230;&#x6211;&#x4EEC;&#x7684;&#x961F;&#x5217;&#x4E2D;&#x3002;</p>
<blockquote>
<h4 id="&#x7ED1;&#x5B9A;&#xFF08;binding&#xFF09;&#x5217;&#x8868;">&#x7ED1;&#x5B9A;&#xFF08;binding&#xFF09;&#x5217;&#x8868;</h4>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>rabbitmqctl list_bindings</code> &#x5217;&#x51FA;&#x6240;&#x6709;&#x73B0;&#x5B58;&#x7684;&#x7ED1;&#x5B9A;&#x3002;</p>
</blockquote>
<h2 id="&#x4EE3;&#x7801;&#x6574;&#x5408;">&#x4EE3;&#x7801;&#x6574;&#x5408;</h2>
<p><img src="http://www.rabbitmq.com/img/tutorials/python-three-overall.png" alt=""></p>
<p>&#x53D1;&#x5E03;&#x65E5;&#x5FD7;&#x6D88;&#x606F;&#x7684;&#x7A0B;&#x5E8F;&#x770B;&#x8D77;&#x6765;&#x548C;&#x4E4B;&#x524D;&#x7684;&#x6CA1;&#x6709;&#x592A;&#x5927;&#x533A;&#x522B;&#x3002;&#x6700;&#x91CD;&#x8981;&#x7684;&#x6539;&#x53D8;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x628A;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x7ED9;logs&#x4EA4;&#x6362;&#x673A;&#x800C;&#x4E0D;&#x662F;&#x533F;&#x540D;&#x4EA4;&#x6362;&#x673A;&#x3002;&#x5728;&#x53D1;&#x9001;&#x7684;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x63D0;&#x4F9B;<code>routing_key</code>&#x53C2;&#x6570;&#xFF0C;&#x4F46;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x5B83;&#x7684;&#x503C;&#x3002;&#x4EE5;&#x4E0B;&#x662F;<code>emit_log.go</code>&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
        <span class="hljs-string">&quot;log&quot;</span>
        <span class="hljs-string">&quot;os&quot;</span>
        <span class="hljs-string">&quot;strings&quot;</span>

        <span class="hljs-string">&quot;github.com/streadway/amqp&quot;</span>
)

<span class="hljs-keyword">func</span> failOnError(err error, msg <span class="hljs-keyword">string</span>) {
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
                log.Fatalf(<span class="hljs-string">&quot;%s: %s&quot;</span>, msg, err)
        }
}

<span class="hljs-keyword">func</span> main() {
        conn, err := amqp.Dial(<span class="hljs-string">&quot;amqp://guest:guest@localhost:5672/&quot;</span>)
        failOnError(err, <span class="hljs-string">&quot;Failed to connect to RabbitMQ&quot;</span>)
        <span class="hljs-keyword">defer</span> conn.Close()

        ch, err := conn.Channel()
        failOnError(err, <span class="hljs-string">&quot;Failed to open a channel&quot;</span>)
        <span class="hljs-keyword">defer</span> ch.Close()

        err = ch.ExchangeDeclare(
                <span class="hljs-string">&quot;logs&quot;</span>,   <span class="hljs-comment">// name</span>
                <span class="hljs-string">&quot;fanout&quot;</span>, <span class="hljs-comment">// type</span>
                <span class="hljs-literal">true</span>,     <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>,    <span class="hljs-comment">// auto-deleted</span>
                <span class="hljs-literal">false</span>,    <span class="hljs-comment">// internal</span>
                <span class="hljs-literal">false</span>,    <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,      <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare an exchange&quot;</span>)

        body := bodyFrom(os.Args)
        err = ch.Publish(
                <span class="hljs-string">&quot;logs&quot;</span>, <span class="hljs-comment">// exchange</span>
                <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// routing key</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// mandatory</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// immediate</span>
                amqp.Publishing{
                        ContentType: <span class="hljs-string">&quot;text/plain&quot;</span>,
                        Body:        []<span class="hljs-keyword">byte</span>(body),
                })
        failOnError(err, <span class="hljs-string">&quot;Failed to publish a message&quot;</span>)

        log.Printf(<span class="hljs-string">&quot; [x] Sent %s&quot;</span>, body)
}

<span class="hljs-keyword">func</span> bodyFrom(args []<span class="hljs-keyword">string</span>) <span class="hljs-keyword">string</span> {
        <span class="hljs-keyword">var</span> s <span class="hljs-keyword">string</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">len</span>(args) &lt; <span class="hljs-number">2</span>) || os.Args[<span class="hljs-number">1</span>] == <span class="hljs-string">&quot;&quot;</span> {
                s = <span class="hljs-string">&quot;hello&quot;</span>
        } <span class="hljs-keyword">else</span> {
                s = strings.Join(args[<span class="hljs-number">1</span>:], <span class="hljs-string">&quot; &quot;</span>)
        }
        <span class="hljs-keyword">return</span> s
}
</code></pre>
<p>(<a href="http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/go/emit_log.go" _target="blank">emit_log.go</a> &#x6E90;&#x7801;)</p>
<p>&#x6B63;&#x5982;&#x4F60;&#x770B;&#x5230;&#x7684;&#x90A3;&#x6837;&#xFF0C;&#x5728;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x58F0;&#x660E;&#x4E86;&#x4E00;&#x4E2A;&#x4EA4;&#x6362;&#x5668;&#xFF0C;&#x8FD9;&#x4E00;&#x4E2A;&#x662F;&#x5F88;&#x91CD;&#x8981;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x4E0D;&#x5141;&#x8BB8;&#x53D1;&#x5E03;&#x6D88;&#x606F;&#x5230;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x4EA4;&#x6362;&#x5668;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7ED1;&#x5B9A;&#x961F;&#x5217;&#x5230;&#x4EA4;&#x6362;&#x5668;&#xFF0C;&#x6D88;&#x606F;&#x5C06;&#x4F1A;&#x4E22;&#x5931;&#x3002;&#x4F46;&#x8FD9;&#x4E2A;&#x6CA1;&#x6709;&#x6240;&#x8C13;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6D88;&#x8D39;&#x8005;&#x76D1;&#x542C;&#xFF0C;&#x90A3;&#x4E48;&#x6D88;&#x606F;&#x5C31;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#x3002;</p>
<p><code>receive_logs.go</code>:&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
        <span class="hljs-string">&quot;log&quot;</span>

        <span class="hljs-string">&quot;github.com/streadway/amqp&quot;</span>
)

<span class="hljs-keyword">func</span> failOnError(err error, msg <span class="hljs-keyword">string</span>) {
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
                log.Fatalf(<span class="hljs-string">&quot;%s: %s&quot;</span>, msg, err)
        }
}

<span class="hljs-keyword">func</span> main() {
        conn, err := amqp.Dial(<span class="hljs-string">&quot;amqp://guest:guest@localhost:5672/&quot;</span>)
        failOnError(err, <span class="hljs-string">&quot;Failed to connect to RabbitMQ&quot;</span>)
        <span class="hljs-keyword">defer</span> conn.Close()

        ch, err := conn.Channel()
        failOnError(err, <span class="hljs-string">&quot;Failed to open a channel&quot;</span>)
        <span class="hljs-keyword">defer</span> ch.Close()

        err = ch.ExchangeDeclare(
                <span class="hljs-string">&quot;logs&quot;</span>,   <span class="hljs-comment">// name</span>
                <span class="hljs-string">&quot;fanout&quot;</span>, <span class="hljs-comment">// type</span>
                <span class="hljs-literal">true</span>,     <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>,    <span class="hljs-comment">// auto-deleted</span>
                <span class="hljs-literal">false</span>,    <span class="hljs-comment">// internal</span>
                <span class="hljs-literal">false</span>,    <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,      <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare an exchange&quot;</span>)

        q, err := ch.QueueDeclare(
                <span class="hljs-string">&quot;&quot;</span>,    <span class="hljs-comment">// name</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// delete when usused</span>
                <span class="hljs-literal">true</span>,  <span class="hljs-comment">// exclusive</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,   <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare a queue&quot;</span>)

        err = ch.QueueBind(
                q.Name, <span class="hljs-comment">// queue name</span>
                <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// routing key</span>
                <span class="hljs-string">&quot;logs&quot;</span>, <span class="hljs-comment">// exchange</span>
                <span class="hljs-literal">false</span>,
                <span class="hljs-literal">nil</span>,
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to bind a queue&quot;</span>)

        msgs, err := ch.Consume(
                q.Name, <span class="hljs-comment">// queue</span>
                <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// consumer</span>
                <span class="hljs-literal">true</span>,   <span class="hljs-comment">// auto-ack</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// exclusive</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-local</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,    <span class="hljs-comment">// args</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to register a consumer&quot;</span>)

        forever := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">bool</span>)

        <span class="hljs-keyword">go</span> <span class="hljs-keyword">func</span>() {
                <span class="hljs-keyword">for</span> d := <span class="hljs-keyword">range</span> msgs {
                        log.Printf(<span class="hljs-string">&quot; [x] %s&quot;</span>, d.Body)
                }
        }()

        log.Printf(<span class="hljs-string">&quot; [*] Waiting for logs. To exit press CTRL+C&quot;</span>)
        &lt;-forever
}
</code></pre>
<p>(<a href="https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/go/receive_logs.go" _target="blank">receive_logs.go</a> &#x6E90;&#x7801;)</p>
<p>&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x5C31;&#x5B8C;&#x6210;&#x4E86;&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x628A;&#x65E5;&#x5FD7;&#x4FDD;&#x5B58;&#x5230;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x6253;&#x5F00;&#x63A7;&#x5236;&#x53F0;&#x8F93;&#x5165;&#xFF1A;</p>
<pre><code>$ go run receive_logs.go &gt; logs_from_rabbit.log
</code></pre><p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5728;&#x5C4F;&#x5E55;&#x4E2D;&#x67E5;&#x770B;&#x65E5;&#x5FD7;&#xFF0C;&#x90A3;&#x4E48;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7EC8;&#x7AEF;&#x7136;&#x540E;&#x8FD0;&#x884C;&#xFF1A;</p>
<pre><code>$ go run receive_logs.go
</code></pre><p>&#x5F53;&#x7136;&#x8FD8;&#x8981;&#x53D1;&#x9001;&#x65E5;&#x5FD7;&#xFF1A;</p>
<pre><code>$ go run emit_log.go
</code></pre><p>&#x4F7F;&#x7528;<code>rabbitmqctl list_bindings</code>&#x4F60;&#x53EF;&#x786E;&#x8BA4;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&#x7684;&#x961F;&#x5217;&#x7ED1;&#x5B9A;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x8FD0;&#x884C;&#x4E2D;&#x7684;&#x4E24;&#x4E2A;<code>receive_logs.go</code>&#x7A0B;&#x5E8F;&#xFF1A;</p>
<pre><code> sudo rabbitmqctl list_bindings
# =&gt; Listing bindings ...
# =&gt; logs    exchange        amq.gen-JzTY20BRgKO-HjmUJj0wLg  queue           []
# =&gt; logs    exchange        amq.gen-vso0PVvyiRIL2WoV3i48Yg  queue           []
# =&gt; ...done.
</code></pre><p>&#x663E;&#x793A;&#x7ED3;&#x679C;&#x5F88;&#x76F4;&#x89C2;&#xFF1A;logs&#x4EA4;&#x6362;&#x5668;&#x628A;&#x6570;&#x636E;&#x53D1;&#x9001;&#x7ED9;&#x4E24;&#x4E2A;&#x7CFB;&#x7EDF;&#x547D;&#x540D;&#x7684;&#x961F;&#x5217;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x6240;&#x671F;&#x671B;&#x7684;&#x3002;</p>
<p>&#x5982;&#x4F55;&#x76D1;&#x542C;&#x6D88;&#x606F;&#x7684;&#x5B50;&#x96C6;&#x5462;&#xFF1F;&#x8BA9;&#x6211;&#x4EEC;&#x79FB;&#x6B65;&#x6559;&#x7A0B;4</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="[2]Work_Queues.html" class="navigation navigation-prev " aria-label="Previous page: 工作队列">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="[4]Routing.html" class="navigation navigation-next " aria-label="Next page: 路由">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"发布/订阅","level":"1.5.3.3","depth":3,"next":{"title":"路由","level":"1.5.3.4","depth":3,"path":"tutorials_with_golang/[4]Routing.md","ref":"tutorials_with_golang/[4]Routing.md","articles":[]},"previous":{"title":"工作队列","level":"1.5.3.2","depth":3,"path":"tutorials_with_golang/[2]Work_Queues.md","ref":"tutorials_with_golang/[2]Work_Queues.md","articles":[]},"dir":"ltr"},"config":{"plugins":["ga","expandable-chapters-interactive","-lunr","-search","search-plus"],"root":"./published","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"ga":{"token":"UA-120619814-1","configuration":"auto"},"expandable-chapters-interactive":{},"search-plus":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Ping <yuanta11@gmil.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"RabbitMQ in Chinese","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{"瓶先生":"http://mr-ping.com"}},"gitbook":"=3.0.0-pre.14","description":"RabbitMQ in Chinese"},"file":{"path":"tutorials_with_golang/[3]Publish_Subscribe.md","mtime":"2020-10-30T03:40:00.900Z","type":"markdown"},"gitbook":{"version":"3.0.0-pre.14","time":"2020-10-30T03:40:06.094Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-ga/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/buttons.js"></script>
        
    

    </body>
</html>

